package crmdna.client.member;

import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.annotation.Nullable;
import javax.inject.Named;

import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiMethod.HttpMethod;
import com.google.appengine.api.users.User;

import crmdna.client.contactdetail.ContactDetail.ContactDetailProp;
import crmdna.client.member.InteractionCore.InteractionProp;
import crmdna.client.member.InteractionCore.InteractionType;
import crmdna.client.member.MemberCore.MemberProp;
import crmdna.common.APIResponse;
import crmdna.common.APIResponse.Status;
import crmdna.common.Utils;

@Api(name = "member")
public class MemberApi {
	@ApiMethod(path="create", httpMethod = HttpMethod.POST)
	public APIResponse create(@Named("client") String client, 
			@Named("groupId") long groupId, @Nullable @Named("email") String email, 
			@Nullable @Named("firstName") String firstName, 
			@Nullable @Named("lastName") String lastName,
			@Nullable @Named("mobilePhone") String mobilePhone, 
			@Nullable @Named("homePhone") String homePhone, 
			@Nullable @Named("officePhone") String officePhone, 
			@Nullable @Named("homeAddress") String homeAddress, 
			@Nullable @Named("officeAddress") String officeAddress, User user) {
		
		try {
			ContactDetailProp contact = new ContactDetailProp();
			contact.email = email;
			contact.firstName = firstName;
			contact.lastName = lastName;
			contact.mobilePhone = mobilePhone;
			contact.homePhone = homePhone;
			contact.officePhone = officePhone;
			contact.homeAddress = homeAddress;
			contact.officeAddress = officeAddress;
					
			MemberProp memberProp = Member.create(client, groupId, contact, 
					Utils.getLoginEmail(user));						
								
			return new APIResponse(Status.SUCCESS, memberProp);
			
		} catch (Exception ex) {
			return new APIResponse(ex);
		}		
	}
	
	@ApiMethod(path="updateContactDetails", httpMethod = HttpMethod.POST)
	public APIResponse updateContactDetails(@Named("client") String client, 
			@Named("memberId") long memberId, @Nullable @Named("email") String email, 
			@Nullable @Named("firstName") String firstName, 
			@Nullable @Named("lastName") String lastName,
			@Nullable @Named("mobilePhone") String mobilePhone, 
			@Nullable @Named("homePhone") String homePhone, 
			@Nullable @Named("officePhone") String officePhone, 
			@Nullable @Named("homeAddress") String homeAddress, 
			@Nullable @Named("officeAddress") String officeAddress, User user) {
		
		try {
			ContactDetailProp contact = new ContactDetailProp();
			contact.email = email;
			contact.firstName = firstName;
			contact.lastName = lastName;
			contact.mobilePhone = mobilePhone;
			contact.homePhone = homePhone;
			contact.officePhone = officePhone;
			contact.homeAddress = homeAddress;
			contact.officeAddress = officeAddress;
					
			MemberProp memberProp = Member.updateContactDetails(client, memberId, contact, 
					Utils.getLoginEmail(user));					
								
			return new APIResponse(Status.SUCCESS, memberProp);
			
		} catch (Exception ex) {
			return new APIResponse(ex);
		}		
	}
	
	@ApiMethod(path="get", httpMethod = HttpMethod.GET)
	public APIResponse get(@Named("client") String client, 
			@Named("memberId") long memberId, User user) {
		
		try {								
			MemberProp memberProp = Member.safeGet(client, memberId, Utils.getLoginEmail(user)).toProp();
			
			return new APIResponse(Status.SUCCESS, memberProp);			
		} catch (Exception ex) {
			return new APIResponse(ex);
		}		
	}
	
	@ApiMethod(path="getAllForGroup", httpMethod = HttpMethod.GET)
	public APIResponse getAllForGroup(@Named("client") String client, 
			@Named("groupId") long groupId, User user) {
		
		try {						
			List<MemberProp> memberProps = Member.getAllForGroup(client, groupId, Utils.getLoginEmail(user));
			
			return new APIResponse(Status.SUCCESS, memberProps);			
		} catch (Exception ex) {
			return new APIResponse(ex);
		}		
	}
	
	@ApiMethod(path="query", httpMethod = HttpMethod.GET)
	public APIResponse query(@Named("client") String client, 
			@Nullable @Named("searchStr") String searchString, 
			@Nullable @Named("groupIds") Set<Long> groupIds, 
			@Nullable @Named("practiceIds") Set<Long> practiceIds, 
			@Nullable @Named("maxResultSize") Integer maxResultsSize, 
			User user) {
		
		try {			
			List<MemberProp> memberProps = Member.query(client, searchString, groupIds, 
					practiceIds, maxResultsSize, Utils.getLoginEmail(user));	
			
			return new APIResponse(Status.SUCCESS, memberProps);			
		} catch (Exception ex) {
			return new APIResponse(ex);
		}		
	}
	
	@ApiMethod(path="addOrDeleteProgram", httpMethod = HttpMethod.POST)
	public APIResponse addOrDeleteProgram(@Named("client") String client, 
			@Named("memberId") long memberId, 
			@Named("programId") long programId, 
			@Named("add") Boolean add, 			
			User user) {
		
		try {			
			MemberProp memberProp = Member.addOrDeleteProgram(client, memberId, 
					programId, add, Utils.getLoginEmail(user)) ;				
			
			return new APIResponse(Status.SUCCESS, memberProp);			
		} catch (Exception ex) {
			return new APIResponse(ex);
		}		
	}
	
	
	
	@ApiMethod(path="addInteraction", httpMethod = HttpMethod.POST)
	public APIResponse addInteraction(@Named("client") String client, 
			@Named("memberId") long memberId, 
			@Named("interaction") String interaction,
			@Named("interactionType") InteractionType interactionType,
			@Nullable @Named("timestamp") Date timestamp, User user) {
		
		try {
			
			InteractionProp interactionProp = Member.addInteraction(client, memberId, interaction, 
					interactionType, timestamp, Utils.getLoginEmail(user));
						
			return new APIResponse(Status.SUCCESS, interactionProp);			
		} catch (Exception ex) {
			return new APIResponse(ex);
		}		
	}
	
	@ApiMethod(path="getAllInteractions", httpMethod = HttpMethod.GET)
	public APIResponse getAllInteractions(@Named("client") String client, 
			@Named("memberId") long memberId, User user) {
		
		try {
			
			List<InteractionProp> interactions = Member.getAllInteractions(client, memberId, 
					Utils.getLoginEmail(user));
						
			return new APIResponse(Status.SUCCESS, interactions);			
		} catch (Exception ex) {
			return new APIResponse(ex);
		}		
	}
	
	@ApiMethod(path="deleteInteraction", httpMethod = HttpMethod.DELETE)
	public APIResponse deleteInteraction(@Named("client") String client, 
			@Named("memberId") long memberId,
			@Named("interactionId") long interactionId, User user) {
		
		try {
			
			Member.deleteInteraction(client, memberId, interactionId, 
					Utils.getLoginEmail(user));						
						
			return new APIResponse(Status.SUCCESS, "interaction deleted");			
		} catch (Exception ex) {
			return new APIResponse(ex);
		}		
	}
}
